package com.zhijian.wharf.config;

import com.zhijian.wharf.annotation.CurrentWebUser;
import com.zhijian.wharf.enums.SystemEnum;
import com.zhijian.wharf.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 后台用户
 *
 * @author HCG
 * @version 1.0
 * @date 2021/3/9
 */
@Component
public class CurrentUserResolver implements HandlerMethodArgumentResolver {

    /**
     * 缓存
     */
    @Autowired
    private TokenService tokenService;

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(CurrentWebUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, @Nullable ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest, @Nullable WebDataBinderFactory webDataBinderFactory) throws Exception {
        return tokenService.getUserDtoByToken(SystemEnum.WEB, nativeWebRequest.getHeader(Constants.wharf_WEB_TOKEN));
    }

}
